<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>添加管理员</title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" href="../static/css/font.css">
    <link rel="stylesheet" href="../static/css/xadmin.css">
      <script type="text/javascript" src="../static/js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="../static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../static/js/xadmin.js"></script>
  </head>
  
  <body>
    <div class="x-body layui-anim layui-anim-up">
        <form class="layui-form">
          <div class="layui-form-item">
              <label for="" class="layui-form-label">
                  <span class="x-red">*</span>用户名
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="admin" name="admin" required="" lay-verify="nikename"
                  autocomplete="off" onchange="adminYZ()" class="layui-input">
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="x-red" id="message">*</span>&nbsp;&nbsp;&nbsp;将会成为您唯一的登入名
              </div>
          </div>
            <div class="layui-form-item">
              <label for="L_pass" class="layui-form-label">
                  <span class="x-red">*</span>密码
              </label>
              <div class="layui-input-inline">
                  <input type="password" id="pass" name="pass" required="" lay-verify="pass"
                  autocomplete="off" class="layui-input">
              </div>
              <div class="layui-form-mid layui-word-aux">
                  6到16个字符
              </div>
          </div>
          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
                  <span class="x-red">*</span>确认密码
              </label>
              <div class="layui-input-inline">
                  <input type="password" id="repass" name="repass" required="" lay-verify="repass"
                  autocomplete="off" class="layui-input">
              </div>
          </div>
          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
              </label>
              <button  class="layui-btn" lay-filter="add" lay-submit="">
                  增加
              </button>
          </div>
      </form>
    </div>
    <script>
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
          var form = layui.form
          ,layer = layui.layer;
        
          //自定义验证规则
          form.verify({
            nikename: function(value){
              if(value.length < 2){
                return '账号至少得2个字符啊';
              }

            }
            ,pass: [/(.+){6,12}$/, '密码必须6到12位']
            ,repass: function(value){
                if($('#pass').val()!=$('#repass').val()){
                    return '两次密码不一致';
                }
            }
          });

          //监听提交
          form.on('submit(add)', function(){
              $admin=$('#admin').val();
              $pass=$('#pass').val();

             $.ajax({
                type:'post',
                cache:'false',
                url:'../php/admin/add_user.php',
                data:{
                  "admin":$admin,
                  "pass":$pass,
                },
                 dataType:'json',
                 success:function (dat) {
                    layer.msg(dat.message);


                 },
                 error:function () {
                    layer.alert("添加失败！",{icon:2});

                 }


             });

            return false;
          });
          
          
        });

        function adminYZ() {
            $admin=$('#admin').val();
            $.ajax({
                type:'post',
                cache:'false',
                url:'../php/admin/queryAdmin.php',
                data:{
                    "admin":$admin,
                },
                dataType:'json',
                success:function () {

                    var mes=document.getElementById("message");
                    mes.innerText="该账号已经存在";
                    mes.style.color="red";
                },
                error:function () {
                    var mes=document.getElementById("message");
                    mes.innerHTML="账号可用";
                    mes.style.color="green";
                }

            })


        }

    </script>
  </body>

</html>